/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;

import java.io.Serializable;
import java.util.Comparator;
import javax.swing.Icon;

/**
 *
 * @author 75
 */
public class Nation implements Serializable,  Comparator<Nation> {

   private String name;
   private String code;
   private LinkedList<AlternateNationCodes> alternateNationCodes;
   private LinkedList<Medal> nationMedals = new LinkedList();

   public Nation() {
   }

   public Nation(String name, String code) {
      this(name, code, new LinkedList<AlternateNationCodes>());
   }

   public Nation(String name, String code, LinkedList<AlternateNationCodes> alternateNationCodes) {
      this.name = name;
      this.code = code;
      this.alternateNationCodes = alternateNationCodes;
   }

   /**
    * @return the name
    */
   public String getName() {
      return name;
   }

   /**
    * @return the code
    */
   public String getCode() {
      return code;
   }

   /**
    * @param name the name to set
    */
   public void setName(String name) {
      this.name = name;
   }

   /**
    * @param code the code to set
    */
   public void setCode(String code) {
      if (code.trim().length() > 3) {
	 code = "";
      } else {
	 this.code = code.toUpperCase();
      }
   }

   /**
    * @return the alternateCodes
    */
   public LinkedList<AlternateNationCodes> getAlternateNationCodes() {
      return alternateNationCodes;
   }

   /**
    * @param alternateCodes the alternateCodes to set
    */
   public void setAlternateNationCodes(LinkedList<AlternateNationCodes> alternateNationCodes) {
      this.alternateNationCodes = alternateNationCodes;
   }

   public void addAlternateCode(AlternateNationCodes alternateNationCode) {
      this.alternateNationCodes.add(alternateNationCode);
   }

   /**
    * @return the nationMedals
    */
   public LinkedList<Medal> getNationMedals() {
      return nationMedals;
   }

   /**
    * @param nationMedals the nationMedals to set
    */
   public void setNationMedals(LinkedList<Medal> nationMedals) {
      this.nationMedals = nationMedals;
   }

   public void addMedal(Medal m) {
      nationMedals.add(m);
   }

   
   public int getTotalMedals(){
      return nationMedals.size();
   }
   
   public int getTotalEditionMedals(Edition e){
      int counter = 0;
      for (Medal m : nationMedals) {
	 if (m.getEdition().equals(e))
	    counter++;
      }
      return counter;
   }
   
    public LinkedList<Medal> getModalityMedals(Modality m){
      LinkedList<Medal> medals = new LinkedList<>();
      for (Medal med : nationMedals) {
	 if(med.getModality().equals(m))
	    medals.add(med);
      }
      return medals;
   }
   
    public LinkedList<Medal> getDisciplineMedals(Discipline d){
      LinkedList<Medal> medals = new LinkedList<>();
      for (Medal med : nationMedals) {
	 if(med.getDiscipline().equals(d))
	    medals.add(med);
      }
      return medals;
   }
   
   
   @Override
   public String toString() {
      return (name + " - " + code + alternateNationCodes.toString());
   }

   @Override
   public int compare(Nation o1, Nation o2) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   


   

   public static class MedalsAbsoluteComparator implements Comparator {

      public int compare(Object o1, Object o2) {
	 if (!(o1 instanceof Nation) || !(o2 instanceof Nation)) {
	    throw new ClassCastException();
	 }


	 Nation n1 = (Nation) o1;
	 Nation n2 = (Nation) o2;

	 return (int) (n2.getNationMedals().size() - n1.getNationMedals().size());
      }
   }
   
   
  public int getMedals(Medal.MEDALTYPE medalType) {
      int counter = 0;
      for (Medal m : nationMedals) {
	 if (m.getMedalType().equals(medalType)) {
	    counter++;
	 }
      }
      return counter;
   }
  
  
  
}
